package com.neuedu.ssm.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neuedu.ssm.entity.User;
import com.neuedu.ssm.entity.UserExample;
import com.neuedu.ssm.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    UserMapper userMapper;


    /**
     * 根据条件查询
     * @param username
     * @return
     */
    public List<User> selectByCondition(String username){

        UserExample userExample = new UserExample();

        if(username!= null && username.length()>0){
            userExample.createCriteria().andUsernameLike("%"+username+"%");
        }

        List<User> userList = userMapper.selectByExample(userExample);

        return userList;
    }


    /**
     * 根据条件查询 ，带分页
     * @param username
     * @param page
     * @return
     */
    public Page selectByConditionPage(String username, Page page){

        UserExample userExample = new UserExample();

        if(username!= null && username.length()>0){
            userExample.createCriteria().andUsernameLike("%"+username+"%");
        }

        //开启分页查询
        page = PageHelper.startPage(page.getPageNum(), page.getPageSize());
        userMapper.selectByExample(userExample);

        return page;
    }


    /**
     * 添加用户信息

     * @return
     */
    @Transactional(value="txManager", rollbackFor = Exception.class )
    public boolean save(int a ,int b){

        User user = new User();
        user.setUsername("admin");
        user.setPassword("123456");
        int count = userMapper.insert(user);

        int result = a/b;

        user = new User();
        user.setUsername("root");
        user.setPassword(""+result);
        int count2 = userMapper.insert(user);

        return count>0 && count2>0;
    }


}
